using DeviceCommons.DeviceMessages.Enums;

namespace DeviceCommons.DeviceMessages.Factories
{
    /// <summary>
    /// 设备状态配置构建器
    /// 提供流畅的API来定义设备状态配置
    /// </summary>
    public class DeviceStateConfigurationBuilder
    {
        private readonly byte _deviceType;
        private readonly List<StateDefinition> _stateDefinitions = new();

        public DeviceStateConfigurationBuilder(byte deviceType)
        {
            _deviceType = deviceType;
        }

        /// <summary>
        /// 添加状态定义
        /// </summary>
        /// <param name="sid">状态ID</param>
        /// <param name="valueType">值类型</param>
        /// <param name="name">状态名称（可选）</param>
        /// <param name="description">状态描述（可选）</param>
        /// <param name="isRequired">是否必需</param>
        /// <param name="defaultValue">默认值（可选）</param>
        /// <param name="validationRule">验证规则（可选）</param>
        /// <returns>构建器实例</returns>
        public DeviceStateConfigurationBuilder AddState(
            byte sid,
            StateValueTypeEnum valueType,
            string? name = null,
            string? description = null,
            bool isRequired = true,
            object? defaultValue = null,
            Func<object, bool>? validationRule = null)
        {
            var stateDef = new StateDefinition(sid, valueType, name, description, isRequired, defaultValue, validationRule);
            _stateDefinitions.Add(stateDef);
            return this;
        }

        /// <summary>
        /// 添加字符串状态
        /// </summary>
        /// <param name="sid">状态ID</param>
        /// <param name="name">状态名称（可选）</param>
        /// <param name="description">状态描述（可选）</param>
        /// <param name="isRequired">是否必需</param>
        /// <param name="defaultValue">默认值（可选）</param>
        /// <param name="validationRule">验证规则（可选）</param>
        /// <returns>构建器实例</returns>
        public DeviceStateConfigurationBuilder AddStringState(
            byte sid,
            string? name = null,
            string? description = null,
            bool isRequired = true,
            string? defaultValue = null,
            Func<object, bool>? validationRule = null)
        {
            return AddState(sid, StateValueTypeEnum.String, name, description, isRequired, defaultValue, validationRule);
        }

        /// <summary>
        /// 添加Int32状态
        /// </summary>
        /// <param name="sid">状态ID</param>
        /// <param name="name">状态名称（可选）</param>
        /// <param name="description">状态描述（可选）</param>
        /// <param name="isRequired">是否必需</param>
        /// <param name="defaultValue">默认值（可选）</param>
        /// <param name="validationRule">验证规则（可选）</param>
        /// <returns>构建器实例</returns>
        public DeviceStateConfigurationBuilder AddInt32State(
            byte sid,
            string? name = null,
            string? description = null,
            bool isRequired = true,
            int? defaultValue = null,
            Func<object, bool>? validationRule = null)
        {
            return AddState(sid, StateValueTypeEnum.Int32, name, description, isRequired, defaultValue, validationRule);
        }

        /// <summary>
        /// 添加Int16状态
        /// </summary>
        /// <param name="sid">状态ID</param>
        /// <param name="name">状态名称（可选）</param>
        /// <param name="description">状态描述（可选）</param>
        /// <param name="isRequired">是否必需</param>
        /// <param name="defaultValue">默认值（可选）</param>
        /// <param name="validationRule">验证规则（可选）</param>
        /// <returns>构建器实例</returns>
        public DeviceStateConfigurationBuilder AddInt16State(
            byte sid,
            string? name = null,
            string? description = null,
            bool isRequired = true,
            short? defaultValue = null,
            Func<object, bool>? validationRule = null)
        {
            return AddState(sid, StateValueTypeEnum.Int16, name, description, isRequired, defaultValue, validationRule);
        }

        /// <summary>
        /// 添加UInt16状态
        /// </summary>
        /// <param name="sid">状态ID</param>
        /// <param name="name">状态名称（可选）</param>
        /// <param name="description">状态描述（可选）</param>
        /// <param name="isRequired">是否必需</param>
        /// <param name="defaultValue">默认值（可选）</param>
        /// <param name="validationRule">验证规则（可选）</param>
        /// <returns>构建器实例</returns>
        public DeviceStateConfigurationBuilder AddUInt16State(
            byte sid,
            string? name = null,
            string? description = null,
            bool isRequired = true,
            ushort? defaultValue = null,
            Func<object, bool>? validationRule = null)
        {
            return AddState(sid, StateValueTypeEnum.UInt16, name, description, isRequired, defaultValue, validationRule);
        }

        /// <summary>
        /// 添加Float32状态
        /// </summary>
        /// <param name="sid">状态ID</param>
        /// <param name="name">状态名称（可选）</param>
        /// <param name="description">状态描述（可选）</param>
        /// <param name="isRequired">是否必需</param>
        /// <param name="defaultValue">默认值（可选）</param>
        /// <param name="validationRule">验证规则（可选）</param>
        /// <returns>构建器实例</returns>
        public DeviceStateConfigurationBuilder AddFloat32State(
            byte sid,
            string? name = null,
            string? description = null,
            bool isRequired = true,
            float? defaultValue = null,
            Func<object, bool>? validationRule = null)
        {
            return AddState(sid, StateValueTypeEnum.Float32, name, description, isRequired, defaultValue, validationRule);
        }

        /// <summary>
        /// 添加Double状态（保留原有方法，作为Double类型的快捷方法）
        /// </summary>
        /// <param name="sid">状态ID</param>
        /// <param name="name">状态名称（可选）</param>
        /// <param name="description">状态描述（可选）</param>
        /// <param name="isRequired">是否必需</param>
        /// <param name="defaultValue">默认值（可选）</param>
        /// <param name="validationRule">验证规则（可选）</param>
        /// <returns>构建器实例</returns>
        public DeviceStateConfigurationBuilder AddNumericState(
            byte sid,
            string? name = null,
            string? description = null,
            bool isRequired = true,
            double? defaultValue = null,
            Func<object, bool>? validationRule = null)
        {
            return AddState(sid, StateValueTypeEnum.Double, name, description, isRequired, defaultValue, validationRule);
        }

        /// <summary>
        /// 添加通用数值状态（允许指定具体的数值类型）
        /// </summary>
        /// <param name="sid">状态ID</param>
        /// <param name="numericType">数值类型</param>
        /// <param name="name">状态名称（可选）</param>
        /// <param name="description">状态描述（可选）</param>
        /// <param name="isRequired">是否必需</param>
        /// <param name="defaultValue">默认值（可选）</param>
        /// <param name="validationRule">验证规则（可选）</param>
        /// <returns>构建器实例</returns>
        public DeviceStateConfigurationBuilder AddNumericState(
            byte sid,
            StateValueTypeEnum numericType,
            string? name = null,
            string? description = null,
            bool isRequired = true,
            object? defaultValue = null,
            Func<object, bool>? validationRule = null)
        {
            // 验证是否为数值类型
            if (!IsNumericType(numericType))
            {
                throw new ArgumentException($"类型 {numericType} 不是有效的数值类型。有效的数值类型包括：Int32, Int16, UInt16, Float32, Double");
            }
            
            return AddState(sid, numericType, name, description, isRequired, defaultValue, validationRule);
        }

        /// <summary>
        /// 添加布尔状态
        /// </summary>
        /// <param name="sid">状态ID</param>
        /// <param name="name">状态名称（可选）</param>
        /// <param name="description">状态描述（可选）</param>
        /// <param name="isRequired">是否必需</param>
        /// <param name="defaultValue">默认值（可选）</param>
        /// <param name="validationRule">验证规则（可选）</param>
        /// <returns>构建器实例</returns>
        public DeviceStateConfigurationBuilder AddBooleanState(
            byte sid,
            string? name = null,
            string? description = null,
            bool isRequired = true,
            bool? defaultValue = null,
            Func<object, bool>? validationRule = null)
        {
            return AddState(sid, StateValueTypeEnum.Bool, name, description, isRequired, defaultValue, validationRule);
        }

        /// <summary>
        /// 添加二进制状态
        /// </summary>
        /// <param name="sid">状态ID</param>
        /// <param name="name">状态名称（可选）</param>
        /// <param name="description">状态描述（可选）</param>
        /// <param name="isRequired">是否必需</param>
        /// <param name="defaultValue">默认值（可选）</param>
        /// <param name="validationRule">验证规则（可选）</param>
        /// <returns>构建器实例</returns>
        public DeviceStateConfigurationBuilder AddBinaryState(
            byte sid,
            string? name = null,
            string? description = null,
            bool isRequired = true,
            byte[]? defaultValue = null,
            Func<object, bool>? validationRule = null)
        {
            return AddState(sid, StateValueTypeEnum.Binary, name, description, isRequired, defaultValue, validationRule);
        }

        /// <summary>
        /// 添加时间戳状态
        /// </summary>
        /// <param name="sid">状态ID</param>
        /// <param name="name">状态名称（可选）</param>
        /// <param name="description">状态描述（可选）</param>
        /// <param name="isRequired">是否必需</param>
        /// <param name="defaultValue">默认值（可选）</param>
        /// <param name="validationRule">验证规则（可选）</param>
        /// <returns>构建器实例</returns>
        public DeviceStateConfigurationBuilder AddTimestampState(
            byte sid,
            string? name = null,
            string? description = null,
            bool isRequired = true,
            ulong? defaultValue = null,
            Func<object, bool>? validationRule = null)
        {
            return AddState(sid, StateValueTypeEnum.Timestamp, name, description, isRequired, defaultValue, validationRule);
        }

        /// <summary>
        /// 验证是否为数值类型
        /// </summary>
        /// <param name="type">要验证的类型</param>
        /// <returns>是否为数值类型</returns>
        private bool IsNumericType(StateValueTypeEnum type)
        {
            return type switch
            {
                StateValueTypeEnum.Int32 => true,
                StateValueTypeEnum.Int16 => true,
                StateValueTypeEnum.UInt16 => true,
                StateValueTypeEnum.Float32 => true,
                StateValueTypeEnum.Double => true,
                _ => false
            };
        }

        /// <summary>
        /// 构建设备状态配置
        /// </summary>
        /// <returns>设备状态配置实例</returns>
        public DeviceStateConfiguration Build()
        {
            return new DeviceStateConfiguration(_deviceType, _stateDefinitions);
        }

        /// <summary>
        /// 创建新的设备状态配置构建器
        /// </summary>
        /// <param name="deviceType">设备类型</param>
        /// <returns>构建器实例</returns>
        public static DeviceStateConfigurationBuilder Create(byte deviceType)
        {
            return new DeviceStateConfigurationBuilder(deviceType);
        }
    }
}
